// facility/mapCamera/mapCamera.js
import http from '../../http'
let timer;
Component({
	/**
	 * 组件的属性列表
	 */
	properties: {
		infoType: '',
		nowProjectInfo: '',
		nowfacilityInfo: '',
	},

	/**
	 * 组件的初始数据
	 */
	data: {
		// 摄像头
		cameraBol: false,
		liveUrl: 'wss://81.70.48.111/videos/rtp/34020000001110000001_34020000001320000001.live.flv'
	},

	/**
	 * 组件的方法列表
	 */
	methods: {
		// 摄像头
		play() {
			// this.setData({
			// 	cameraBol: true
			// })
			if (this.properties.infoType == 'project') {
				wx.showToast({
					title: '请选择摄像头后查看',
					icon: 'error',
				})
			} else {
				wx.showLoading({
					title: '加载中...'
				});
				// 请求超时
				timer = setTimeout(() => {
					wx.showToast({
						title: '请求超时',
						icon: 'error',
					})
				}, 7000);
				let sn = this.properties.nowfacilityInfo.sn
				let url = http.camera + '/rtp/' + sn.split(':')[0] + '_' + sn.split(':')[1] + '.live.flv'
				http.get({
					prefix: '/wvp',
					url: "/api/user/login",
					header: {
						"content-type": "application/json"
					},
					data: {
						username: 'admin',
						password: '21232f297a57a5a743894a0e4a801fc3',
					},
					then: (res) => {
						console.log('wvp登录', res);
						let token = res.data.data.accessToken
						http.get({
							prefix: '/wvp',
							url: "/api/play/start/" + sn.split(':')[0] + '/' + sn.split(':')[1],
							header: {
								"content-type": "application/json",
								'access-token': token
							},
							then: (res) => {
								console.log('打开摄像头流', res);
								clearTimeout(timer)
								wx.hideLoading()
								wx.navigateTo({
									url: `../live/live?url=${url}&cameraSn=${sn}&token=${token}`,
								})
							}
						})
					}
				})

			}
		},
		statechange(e) {
			console.log('live-player code:', e.detail.code)
		},
		error(e) {
			console.error('live-player error:', e.detail.errMsg)
		},
		cameraClose() {
			this.setData({
				cameraBol: false
			})
		},
	}
})